package com.acane.instagram.core.model.network.datasource

import com.acane.instagram.core.config.Config
import com.acane.instagram.core.model.Notification
import com.acane.instagram.core.model.PostInfo
import com.acane.instagram.core.model.Tag
import com.acane.instagram.core.model.UserInfo
import com.acane.instagram.core.model.dto.CommentDTO
import com.acane.instagram.core.model.dto.CommentLikeDTO
import com.acane.instagram.core.model.dto.FollowDTO
import com.acane.instagram.core.model.dto.LikeDTO
import com.acane.instagram.core.model.network.di.NetworkModule
import com.acane.instagram.core.model.network.retrofit.MyNetworkApiService
import com.acane.instagram.core.model.request.SensitiveCheckRequest
import com.acane.instagram.core.model.response.NetworkResponse
import com.acane.instagram.feature.login.LoginResponse
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.http.Body
import retrofit2.http.Multipart
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query

//
//class AuthInterceptor(private val context: Context) : Interceptor {
//    override fun intercept(chain: Interceptor.Chain): Response {
//        val originalRequest = chain.request()
//        val token = SessionManager.getJwtToken()
//
//        val requestBuilder = originalRequest.newBuilder()
//        if (token != null) {
//            requestBuilder.addHeader("Authorization", "Bearer $token")
//        }
//
//        val request = requestBuilder.build()
//        return chain.proceed(request)
//    }
//}
object MyRetrofitDatasource {
    private val retrofitApiService = Retrofit.Builder()
        .baseUrl(Config.ADDRESS)
        .callFactory(NetworkModule.providesOkHttpClient())
        .addConverterFactory(NetworkModule.providesNetworkJson().asConverterFactory("application/json".toMediaType()))
        .build()
        .create(MyNetworkApiService::class.java)
//private lateinit var retrofitApiService: MyNetworkApiService
//
//    fun init(context: Context) {
//        val okHttpClient = OkHttpClient.Builder()
//            .addInterceptor(AuthInterceptor(context))
//            .build()
//
//        retrofitApiService = Retrofit.Builder()
//            .baseUrl(Config.ADDRESS)
//            .client(okHttpClient)
//            .addConverterFactory(NetworkModule.providesNetworkJson().asConverterFactory("application/json".toMediaType()))
//            .build()
//            .create(MyNetworkApiService::class.java)
//    }
    suspend fun login(
        @Query("userName") userName: String?,
        @Query("password") password: String?
    ): LoginResponse {
        return retrofitApiService.login(userName, password)
    }


    // 获取推荐用户列表
    suspend fun getRecommendUserList(
        @Path(value = "userId") userId: String,
    ): NetworkResponse<List<UserInfo>> {
        return retrofitApiService.getRecommendUserList(userId)
    }
    // 获取用户头像
    suspend fun getUserAvatar(
        @Path("userId") userId: String
    ): NetworkResponse<String> {
        return retrofitApiService.getUserAvatar(userId)
    }
    // 获取推荐帖子列表
    suspend fun getRecommendPostList(
        @Query(value = "userId") userId: String,
    ): NetworkResponse<List<PostInfo>> {
        return retrofitApiService.getRecommendPostList(userId)
    }
    // 获取用户帖子列表
    suspend fun getPostListById(
        @Path(value = "userId") userId: String,
    ): NetworkResponse<List<PostInfo>> {
        return retrofitApiService.getPostListById(userId)
    }

    suspend fun getUserById(
        @Path("userId") userId: String,
    ): NetworkResponse<UserInfo> {
        return retrofitApiService.getUserById(userId)
    }

    suspend fun judgeIsFollowed(
        @Query("followingId") followingId: String,
        @Query("followerId") followerId: String?
    ): NetworkResponse<Boolean> {
        return retrofitApiService.judgeIsFollowed(followingId, followerId)
    }

    suspend fun getChatHistory(
        @Query("sender") sender: String,
        @Query("receiver") receiver: String)
    : NetworkResponse<List<Notification.ChatMessage>> {
        return retrofitApiService.getChatHistory(sender, receiver)
    }

    suspend fun getFriendMessageList(
        @Query("userName") userName: String?
    ): NetworkResponse<List<UserInfo>> {
        return retrofitApiService.getFriendMessageList(userName)
    }

    suspend fun getUserByUserName(
        @Query("userName") chatName: String)
    : NetworkResponse<UserInfo> {
        return retrofitApiService.getUserByUserName(chatName)
    }

    suspend fun uploadMedia(
        @Part file: MultipartBody.Part
    ): NetworkResponse<String> {
        return retrofitApiService.uploadMedia(file)
    }

    suspend fun createNewPost(
        @Body postInfo: PostInfo
    ) : NetworkResponse<PostInfo>{
        return retrofitApiService.createNewPost(postInfo)
    }

    suspend fun getPostPosition(
        @Query("postId") postId: String,
        @Query("userId") userId: String
    ) : NetworkResponse<Int> {
        return retrofitApiService.getPostPosition(postId, userId)
    }

    suspend fun getCommentHistory(
       @Query("userId") userId: String
    ) : NetworkResponse<List<CommentDTO>> {
        return retrofitApiService.getCommentHistory(userId)
    }

    suspend fun getLikeHistory(
        @Query("userId") userId: String
    ) : NetworkResponse<List<LikeDTO>> {
        return retrofitApiService.getLikeHistory(userId)
    }

    suspend fun getFollowHistory(
        @Query("userId") userId: String
    ) : NetworkResponse<List<FollowDTO>> {
        return retrofitApiService.getFollowHistory(userId)
    }

    suspend fun getPostById(
       @Query("postId") postId: String
    ): NetworkResponse<PostInfo> {
        return retrofitApiService.getPostById(postId)
    }

    suspend fun getCommentListByPostId(
       @Query("postId") postId: String
    ): NetworkResponse<List<CommentDTO>> {
        return retrofitApiService.getCommentListByPostId(postId)
    }

    suspend fun createNewComment(
       @Body commentDTO: CommentDTO
    ) : NetworkResponse<CommentDTO>{
        return retrofitApiService.createNewComment(commentDTO)
    }

    suspend fun likePost(
       @Query ("postId") postId: String,
    ): NetworkResponse<Int> {
        return retrofitApiService.likePost(postId)
    }

    suspend fun unlikePost(
        @Query ("postId") postId: String,
    ): NetworkResponse<Int> {
        return retrofitApiService.unlikePost(postId)
    }

    suspend fun likeComment(
        @Query ("commentId") commentId: String,
    ): NetworkResponse<Int> {
        return retrofitApiService.likeComment(commentId)
    }

    suspend fun unLikeComment(
        @Query ("commentId") commentId: String,
    ): NetworkResponse<Int> {
        return retrofitApiService.unLikeComment(commentId)
    }
    suspend fun judgeIsLiked(
       @Query("postId") postId: String,
       @Query("likerId") likerId: String?
    ): NetworkResponse<Boolean> {
       return retrofitApiService.judgeIsLiked(postId, likerId)
    }

    suspend fun sendFollowRequest(
       @Query("followingId") followingId: String,
       @Query("followerId") followerId: String
    ): NetworkResponse<FollowDTO> {
        return retrofitApiService.sendFollowRequest(followingId, followerId)
    }

    suspend fun sendUnFollowRequest(
        @Query("followingId")followingId: String,
        @Query("followerId")followerId: String
    ): NetworkResponse<FollowDTO> {
        return retrofitApiService.sendUnFollowRequest(followingId, followerId)
    }

    suspend fun getCommentById(
        @Query("commentId") commentId: String
    ): NetworkResponse<CommentDTO> {
        return retrofitApiService.getCommentById(commentId)
    }

    suspend fun getCommentLikeHistory(
        @Query("userId") userId: String
    ) : NetworkResponse<List<CommentLikeDTO>> {
        return retrofitApiService.getCommentLikeHistory(userId)
    }

    suspend fun judgeIsCommentLiked(
       @Query("commentId") commentId: String?,
       @Query("likerId") likerId: String?
    ): NetworkResponse<Boolean> {
        return retrofitApiService.judgeIsCommentLiked(commentId, likerId)
    }

    suspend fun getFollowingList(
       @Query("userId") userId: String
    ): NetworkResponse<List<UserInfo>> {
        return retrofitApiService.getFollowingList(userId)
    }

    suspend fun getFollowerList(
        @Query("userId") userId: String
    ): NetworkResponse<List<UserInfo>> {
        return retrofitApiService.getFollowerList(userId)
    }

    suspend fun getRecommendVideoList(
       @Query("userId") userId: String
    ): NetworkResponse<List<PostInfo>> {
        return retrofitApiService.getRecommendVideoList(userId)
    }

    suspend fun userSearchResults(
        @Query("keyword") query: String
    ): NetworkResponse<List<UserInfo>> {
        return retrofitApiService.userSearchResults(query)
    }

    suspend fun postSearchResults(
        @Query("keyword") query: String
    ): NetworkResponse<List<PostInfo>> {
        return retrofitApiService.postSearchResults(query)
    }

    suspend fun getTagList(
    ): NetworkResponse<List<Tag>> {
        return retrofitApiService.getTagList()
    }

    @Multipart
    suspend fun uploadAvatar(
        @Part("userId") userId: RequestBody,
        @Part file: MultipartBody.Part
    ): NetworkResponse<String> {
        return retrofitApiService.uploadAvatar(userId, file)
    }

    suspend fun updateUserInfo(
       @Body userInfo: UserInfo
    ): NetworkResponse<UserInfo> {
        return retrofitApiService.updateUserInfo(userInfo)
    }

    suspend fun register(
        @Body userInfo: UserInfo,
    ): NetworkResponse<UserInfo> {
        return retrofitApiService.register(userInfo)
    }

    suspend fun getCollectionPostList(
       @Query("userId") userId: String
    ): NetworkResponse<List<PostInfo>> {
        return retrofitApiService.getCollectionPostList(userId)
    }

    suspend fun judgeIsCollected(
       @Query("postId") postId: String,
       @Query("userId") userId: String?
    ): NetworkResponse<Boolean> {
        return retrofitApiService.judgeIsCollected(postId, userId)
    }

    suspend fun collectPost(
        @Query("postId") postId: String,
        @Query("userId") userId: String?
    ): NetworkResponse<Boolean> {
        return retrofitApiService.collectPost(postId, userId)
    }

    suspend fun unCollectPost(
        @Query("postId") postId: String,
        @Query("userId") userId: String?
    ): NetworkResponse<Int> {
        return retrofitApiService.unCollectPost(postId, userId)
    }

    suspend fun checkSensitive(
        text: String
    ): NetworkResponse<Boolean> {
        val request = SensitiveCheckRequest(text = text)
        return retrofitApiService.checkSensitive(request)
    }
}